package com.vogue.supplymall.order.controller;

import com.vogue.supplymall.common.domain.dto.ControllerResultInfo;
import com.vogue.supplymall.common.domain.dto.UpdatePrepay;
import com.vogue.supplymall.common.domain.dto.UserInfo;
import com.vogue.supplymall.common.exception.ParamErrorException;
import com.vogue.supplymall.order.common.CheckPrepayInfoUtils;
import com.vogue.supplymall.order.domain.biz.PrepayBiz;
import com.vogue.supplymall.order.exception.DuplicationPrepayException;
import com.vogue.supplymall.order.exception.UpdatePrepayException;
import org.apache.shiro.SecurityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.math.BigDecimal;

/**
 * 预付货款更新用的controller
 *
 * @author zhangliang
 * Create in 2018-08-23
 */
@RestController
public class PrepayUpdateController extends BaseController {
    /**
     * The constant log.
     */
    public static final Logger LOG = LoggerFactory.getLogger(PrepayUpdateController.class);
    @Resource
    private PrepayBiz prepayBiz;

    /**
     * Update prepay for cld response entity.
     * 运供应链调用接口，创建、增加、减少使用此接口
     *
     * @param appver       the appver
     * @param updatePrepay the update prepay
     * @return the response entity
     */
    @RequestMapping(value = "/ns/{appver}/order/prepay/cld/update", method = RequestMethod.POST)
    public ResponseEntity<Object> updatePrepayForCld(@PathVariable("appver") String appver, @RequestBody UpdatePrepay updatePrepay) {
        LOG.info("update prepay for cld start >>>>>> ");
        if (CheckPrepayInfoUtils.checkUpdatePrepayForCldParameter(updatePrepay)) {
            throw new ParamErrorException("");
        }

        try {
            prepayBiz.updatePrepayForCld(updatePrepay);
        } catch (DuplicationPrepayException de) {
            LOG.warn("重复单据，arapbillid={},serialnum={}", updatePrepay.getArapbillid(), updatePrepay.getSerialNum());
        }

        ControllerResultInfo resultInfo = new ControllerResultInfo();
        resultInfo.setResult(true);
        resultInfo.setData(null);
        resultInfo.setMsg("更新账户成功");
        LOG.info(" <<<<<< update prepay for cld end");
        return new ResponseEntity<>(resultInfo, HttpStatus.OK);
    }

    /**
     * create prepay for cld response entity.
     * 运供应链调用接口，创建、增加、减少使用此接口
     *
     * @param appver       the appver
     * @param updatePrepay the update prepay
     * @return the response entity
     */
    @RequestMapping(value = "/ns/{appver}/order/prepay/cld/create", method = RequestMethod.POST)
    public ResponseEntity<Object> createPrepayForCld(@PathVariable("appver") String appver, @RequestBody UpdatePrepay updatePrepay) {
        LOG.info("create prepay for cld start >>>>>> ");
        if (CheckPrepayInfoUtils.checkCreatePrepayForCldParameter(updatePrepay)) {
            throw new ParamErrorException("");
        }

        try {
            updatePrepay.setAmount(BigDecimal.ZERO);
            prepayBiz.createPrepayForCld(updatePrepay);
        } catch (DuplicationPrepayException de) {
            LOG.warn("重复单据，arapbillid={},serialnum={}", updatePrepay.getArapbillid(), updatePrepay.getSerialNum());
        }

        ControllerResultInfo resultInfo = new ControllerResultInfo();
        resultInfo.setResult(true);
        resultInfo.setData(null);
        resultInfo.setMsg("创建账户成功");
        LOG.info(" <<<<<< update create for cld end");
        return new ResponseEntity<>(resultInfo, HttpStatus.OK);
    }

    /**
     * Update prepay response entity.
     * 更新预付货款
     *
     * @param appver       the appver
     * @param updatePrepay the update prepay
     * @return the response entity
     */
    @RequestMapping(value = "/is/{appver}/order/prepay/update", method = RequestMethod.POST)
    public ResponseEntity<Object> updatePrepay(@PathVariable("appver") String appver, @RequestBody UpdatePrepay updatePrepay) {
        LOG.info("update prepayorgan start >>>>>> ");
        if (SecurityUtils.getSubject().isAuthenticated()) {
            UserInfo info = (UserInfo) SecurityUtils.getSubject().getPrincipal();
            if (null != info.getOrganInfo()) {
                updatePrepay.setBuyorganid(info.getOrganInfo().getId());
            } else {
                updatePrepay.setBuyorganid(info.getLoginId());
            }
            updatePrepay.setAccountid(info.getAccountid());
            updatePrepay.setBuysyscodeid(info.getSyscodeid());
        } else {
            LOG.info("获取登信息失败！");
        }

        if (CheckPrepayInfoUtils.checkUpdatePrepayParameter(updatePrepay)) {
            throw new ParamErrorException("");
        }

        try {
            prepayBiz.updatePrepay(updatePrepay);
        } catch (UpdatePrepayException e) {
            throw new UpdatePrepayException("");
        }

        ControllerResultInfo resultInfo = new ControllerResultInfo();
        resultInfo.setResult(true);
        resultInfo.setData(null);
        resultInfo.setMsg("更新账户成功");
        LOG.info(" <<<<<< update prepayorgan end");
        return new ResponseEntity<>(resultInfo, HttpStatus.OK);
    }
}
